﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DataIntegrityChecker.Data;
using System.Resources;

namespace DataIntegrityChecker
{
    public partial class FormSettings : Form
    {
        DataManager dataManager;
        private List<IDataConstraint> constraints;

        public FormSettings(DataManager manager)
        {
            InitializeComponent();
            this.dataManager = manager;
            this.constraints = new List<IDataConstraint>();
            if (dataManager == null)
            {
                tabControl1.TabPages.Remove(tabPageConstraints);
            }
            else
            {
                InitCombobox();
            }
        }

        private void InitCombobox()
        {
            toolStripComboBoxConstraint.Items.Clear();
            toolStripComboBoxConstraint.Items.AddRange(dataManager.Headers);
        }

        public new DialogResult ShowDialog(IWin32Window owner)
        {
            UpdateConstraints();
            return base.ShowDialog(owner);
        }

        private void UpdateConstraints()
        {
            int i = 1;
            listViewConstraints.Items.Clear();
            foreach(IDataConstraint constraint in constraints){
                if(constraint is EqualityConstraint){
                    EqualityConstraint equalityConstraint = (EqualityConstraint) constraint;
                    ListViewItem item = new ListViewItem(new string[]{(i++).ToString(), dataManager.Headers[equalityConstraint.ColumnToBeEqual]});
                    listViewConstraints.Items.Add(item);
                }
            }
        }
        
        internal List<IDataConstraint> Constraints
        {
            get { return constraints; }
            set { constraints = value; UpdateConstraints(); }
        }

        public double CutOff
        {
            get
            {
                return double.Parse(textBoxCutoff.Text);
            }
            set
            {
                textBoxCutoff.Text = value.ToString();
            }
        }

        public string Language
        {
            get
            {
                switch (comboBoxLanguage.SelectedIndex)
                {
                    case 0:
                        return "en-US";
                    case 1:
                        return "nl-NL";
                    default:
                        return "en-US";
                }
            }
            set
            {
                if ("en-US".Equals(value))
                    comboBoxLanguage.SelectedIndex = 0;
                else if ("nl-NL".Equals(value))
                    comboBoxLanguage.SelectedIndex = 1;
                else comboBoxLanguage.SelectedIndex = 0;
            }
        }

        public bool FirstRowHeaders
        {
            get
            {
                return checkBoxFirstRowHeaders.Checked;
            }
            set
            {
                checkBoxFirstRowHeaders.Checked = value;
            }
        }

        private void textBoxCutoff_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = !("0123456789.\b".Contains(e.KeyChar.ToString()));
        }
        
        private void buttonOK_Click(object sender, EventArgs e)
        {
            bool valid = ValidateInput();
            if (valid) this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private bool ValidateInput()
        {
            ResourceManager LocRM = new ResourceManager("DataIntegrityChecker.WinFormStrings", typeof(MainForm).Assembly);
            string cutoffInvalidText = LocRM.GetString("MessageCutoffInvalid");
            string caption = LocRM.GetString("MessageBoxSettingsInvalidCaption");

            string message = "";

            double result = 0.0;
            bool cutoffValid = double.TryParse(textBoxCutoff.Text, out result);
            pictureBoxCutoffError.Visible = !cutoffValid;
            if (!cutoffValid)
            {
                message = cutoffInvalidText + "\n";
            }

            if (message != "")
            {
                MessageBox.Show(message, caption, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            else
            {
                return true;
                
            }
        }

        private void toolStripButtonAddConstraint_Click(object sender, EventArgs e)
        {
            if (toolStripComboBoxConstraint.SelectedIndex == -1) return;
            EqualityConstraint c = new EqualityConstraint(toolStripComboBoxConstraint.SelectedIndex);
            constraints.Add(c);
            UpdateConstraints();
        }

        private void toolStripButtonRemoveConstraint_Click(object sender, EventArgs e)
        {
            if(listViewConstraints.SelectedIndices.Count == 0) return;

            int selected = listViewConstraints.SelectedIndices[0];
            listViewConstraints.Items.RemoveAt(selected);
            constraints.RemoveAt(selected);
        }

    }
}
